use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(CatalogTranslations::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(CatalogTranslations::Id)
                            .string()
                            .not_null()
                            .primary_key(),
                    )
                    .col(
                        ColumnDef::new(CatalogTranslations::CatalogId)
                            .string()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(CatalogTranslations::Locale)
                            .string()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(CatalogTranslations::Title)
                            .string()
                            .not_null(),
                    )
                    .col(ColumnDef::new(CatalogTranslations::Summary).text())
                    .col(ColumnDef::new(CatalogTranslations::MetaTitle).string())
                    .col(ColumnDef::new(CatalogTranslations::MetaDescription).text())
                    .col(ColumnDef::new(CatalogTranslations::MetaKeywords).string())
                    .col(
                        ColumnDef::new(CatalogTranslations::CreatedAt)
                            .timestamp_with_time_zone()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(CatalogTranslations::UpdatedAt)
                            .timestamp_with_time_zone()
                            .default(Expr::current_timestamp()),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_catalog_translations_catalog_id")
                            .from(CatalogTranslations::Table, CatalogTranslations::CatalogId)
                            .to(Catalogs::Table, Catalogs::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(CatalogTranslations::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum CatalogTranslations {
    Table,
    Id,
    CatalogId,
    Locale,
    Title,
    Summary,
    MetaTitle,
    MetaDescription,
    MetaKeywords,
    CreatedAt,
    UpdatedAt,
}

#[derive(DeriveIden)]
enum Catalogs {
    Table,
    Id,
}
